﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HousingInfo.Library;

namespace NEWS.Admin
{
    public partial class Edit_AddNews : System.Web.UI.Page
    {
        private static string mode = "";
        private static NewsBO newsbo;
        protected void Page_Load(object sender, EventArgs e)
        {
            //Mode Add
            if (!IsPostBack)
            {
                dltendanhmuc.DataSource = CategoryInfo.GetCategoryInfo_BySingle(false, false, false, true, true);
                dltendanhmuc.DataBind();

                mode = Request.QueryString["Mode"];
                if (mode != null)
                {
                    pn1.Visible = false;
                    newsbo = NewsBO.GetNewsBO(Convert.ToInt32(mode));
                    dltendanhmuc.SelectedValue = newsbo.CategoryID.ToString();
                    txtnewsid.Text = newsbo.NewsID.ToString();
                    dltendanhmuc.SelectedValue = newsbo.CategoryID.ToString();
                    string ten = dltendanhmuc.SelectedValue;
                    txttitle.Text = newsbo.Title;
                    txtnewscontent.Text = newsbo.NewsContent;
                    lbldatecreated.Text = newsbo.CreatedDate.ToString();
                    if (newsbo.IsActive == true)
                        isactive.Checked = true;
                    else isactive.Checked = false;
                }
                else
                {
                    lblheader.Text = "Đăng tin";
                    pn1.Visible = false;
                    pn2.Visible = false;
                    btnupdate.Text = "Thêm";
                }
            }
        }


        protected void btnback_Click(object sender, EventArgs e)
        {
            Response.Redirect("News.aspx");
        }
        protected void btnupdate_Click(object sender, EventArgs e)
        {
            if (mode != null)
            {   //Chinh sua tin
                int newsid = Convert.ToInt32(txtnewsid.Text);
                newsbo = NewsBO.GetNewsBO(newsid);
                newsbo.Title = txttitle.Text;
                newsbo.CategoryID = Convert.ToInt32(dltendanhmuc.SelectedValue);
                newsbo.NewsContent = txtnewscontent.Text;
                newsbo.CreatedDate = DateTime.Now;
                newsbo.IsActive = isactive.Checked;
                try
                {
                    newsbo.Save();
                    message.Text = "Cập nhật thành công !";
                }
                catch
                {
                    message.Text = "Cập nhật thất bại !";
                }
            }
            else
            {
                //Them moi tin tuc
                newsbo = NewsBO.NewNewsBO();
                newsbo.Title = txttitle.Text;
                newsbo.CategoryID = Convert.ToInt32(dltendanhmuc.SelectedValue);
                newsbo.NewsContent = txtnewscontent.Text;
                newsbo.CreatedDate = DateTime.Now;
                newsbo.ViewCount = 0;
                newsbo.IsActive = isactive.Checked;
                newsbo.ViewCount = 0;
                try
                {
                    newsbo.Save();
                    message.Text = "Thêm thành công";
                    Response.Redirect("News.aspx?Save=Success");
                }
                catch
                {
                    message.Text = "Thêm thất bại";
                }



            }
        }
    }
}